#!/bin/bash
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 11 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


# The package and repository lists are arrays of strings that use the pipe
# character | as a field delimiter. Each field, in turn, is a name=value
# string, which permits flexibility for platform-specific operations.
declare -a -g P_AGENT_PACKAGE_PKGLIST
declare -a -g P_AGENT_PACKAGE_REPOLIST


## p_agent_package_help
##
## Echoes help information for the Package agent. Returns 0.
##
function p_agent_package_help() {
	cat << EOF
Updates system software and installs any additional packages specified by the
subscription. Since software installations and updates are platform-dependent,
this agent does not support all platforms.

In check-only mode, this agent returns 100 if package updates are available,
0 otherwise.
EOF
	return 0
}


## p_agent_package_summary
##
## Echoes summary information for the Package agent. Returns 0.
##
function p_agent_package_summary() {
	echo "Updates and installs system software"
	return 0
}





## p_agent_package_check
##
## Determines if package updates are available, or if new packages should be
## installed. Returns 0 if packages are up to date, 100 if updates are
## available, or another non-zero status if an error occurs.
function p_agent_package_check() {
	local status=0
	local platform=$(get_context platform/name)
	
	if [[ -f "${P_LIBDIR}/platforms/${platform}/package.support" ]]; then
		. "${P_LIBDIR}/platforms/${platform}/package.support"
		p_agent_package_${platform}_check
		status=$?
	else
		echo "Platform not supported for package management."
		status=4
	fi
	
	return ${status}
}


## p_agent_package_run
##
## Runs the Package agent, installing and updating requested software. Returns
## 0 on success, non-zero if an error occurs.
##
function p_agent_package_run() {
	local status=0
	local platform=$(get_context platform/name)
	
	if [[ -f "${P_LIBDIR}/platforms/${platform}/package.support" ]]; then
		. "${P_LIBDIR}/platforms/${platform}/package.support"
		p_agent_package_${platform}_run
		status=$?
	else
		echo "Platform not supported for package management."
		status=4
	fi
	
	return ${status}
}


## add_package [--from repository] <package name>
##
## Adds a package to the list of packages to check, install, or update.
## Returns 0 on success, non-zero on failure.
##
function add_package() {
	local status=0
	if [[ $# -eq 3 ]]; then
		if [[ "$1" == "--from" ]]; then
			P_AGENT_PACKAGE_PKGLIST+=("from=$2|name=$3")
		else
			echo "Invalid options to add_package: $*"
			status=2
		fi
	elif [[ $# -eq 1 ]]; then
		P_AGENT_PACKAGE_PKGLIST+=("name=$1")
	else
		echo "Invalid options to add_package: $*"
		status=2
	fi
	return ${status}
}


## add_repository <option list>
##
## Adds a repository representation. The effects of repositories, and options
## recognized, depend upon the platform on which Pulley is operating.
##
## Returns 0 on success, non-zero otherwise.
##
function add_repository() {
	local status=0
	local entry=""
	local name=""
	
	while [[ $# -gt 0 ]]; do
		if [[ -n "$(echo $1 | grep ^--)" ]]; then
			name=$(echo "$1" | sed s/^--//)
			shift
			entry="${name}=$1"
			shift
		else
			echo "add_repository: invalid option: $1"
			status=2
			break
		fi
	done
	
	if [[ -z "${entry}" ]]; then
		echo "add_repository: missing options"
		status=2
	fi
	
	if [[ ${status} -eq 0 ]]; then
		P_AGENT_PACKAGE_REPOLIST+=("${entry}")
	fi
	
	return ${status}
}
